home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / LSDELCUR.C < prev    next >
Text File  |  1991-09-23  |  3KB  |  142 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)lsdelcur.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10.  
  11. /* library headers */
  12. #include <blkio.h>
  13.  
  14. /* local headers */
  15. #include "lseq_.h"
  16.  
  17. /*man---------------------------------------------------------------------------
  18. NAME
  19.      lsdelcur - delete current lseq record
  20.  
  21. SYNOPSIS
  22.      #include <lseq.h>
  23.  
  24.      int lsdelcur(lsp)
  25.      lseq_t *lsp;
  26.  
  27. DESCRIPTION
  28.      The lsdelcur function deletes the current record from lseq lsp.
  29.      The The cursor is positioned to the record following the deleted
  30.      record.
  31.  
  32.      lsdelcur will fail if one or more of the following is true:
  33.  
  34.      [EINVAL]       lsp is not a valid lseq pointer.
  35.      [LSELOCK]      lsp is not write locked.
  36.      [LSENOPEN]     lsp is not open.
  37.      [LSENREC]      The cursor is null.
  38.  
  39. SEE ALSO
  40.      lsinsert, lssearch.
  41.  
  42. DIAGNOSTICS
  43.      Upon successful completion, a value of 0 is returned.  Otherwise,
  44.      a value of -1 is returned, and errno set to indicate the error.
  45.  
  46. ------------------------------------------------------------------------------*/
  47. #ifdef AC_PROTO
  48. int lsdelcur(lseq_t * lsp)
  49. #else
  50. int lsdelcur(lsp)
  51. lseq_t * lsp;
  52. #endif
  53. {
  54.     bpos_t bpos = 0;
  55.  
  56.     /* validate arguments */
  57.     if (!ls_valid(lsp)) {
  58.         errno = EINVAL;
  59.         return -1;
  60.     }
  61.  
  62.     /* check if not open */
  63.     if (!(lsp->flags & LSOPEN)) {
  64.         errno = LSENOPEN;
  65.         return -1;
  66.     }
  67.  
  68.     /* check if not write locked */
  69.     if (!(lsp->flags & LSWRLCK)) {
  70.         errno = LSELOCK;
  71.         return -1;
  72.     }
  73.  
  74.     /* check if cursor is null */
  75.     if (lsp->clspos == NIL) {
  76.         errno = LSENREC;
  77.         return -1;
  78.     }
  79.  
  80.     /* set modify bit in header */
  81.     lsp->lshdr.flags |= LSHMOD;
  82.     if (bputhf(lsp->bp, sizeof(bpos_t),
  83.         (void *)((char *)&lsp->lshdr + sizeof(bpos_t)),
  84.             sizeof(lshdr_t) - sizeof(bpos_t)) == -1) {
  85.         LSEPRINT;
  86.         return -1;
  87.     }
  88.     if (bsync(lsp->bp) == -1) {
  89.         LSEPRINT;
  90.         return -1;
  91.     }
  92.  
  93.     /* fix links */
  94.     if (lsp->clsrp->next == NIL) {
  95.         lsp->lshdr.last = lsp->clsrp->prev;
  96.     } else {
  97.         if (bputbf(lsp->bp, (bpos_t)lsp->clsrp->next, offsetof(lsrec_t, prev), &lsp->clsrp->prev, sizeof(lsp->clsrp->prev)) == -1) {
  98.             LSEPRINT;
  99.             return -1;
  100.         }
  101.     }
  102.     if (lsp->clsrp->prev == NIL) {
  103.         lsp->lshdr.first = lsp->clsrp->next;
  104.     } else {
  105.         if (bputbf(lsp->bp, (bpos_t)lsp->clsrp->prev, offsetof(lsrec_t, next), &lsp->clsrp->next, sizeof(lsp->clsrp->next)) == -1) {
  106.             LSEPRINT;
  107.             return -1;
  108.         }
  109.     }
  110.  
  111.     /* decrement record count */
  112.     --lsp->lshdr.reccnt;
  113.  
  114.     /* return block to free list */
  115.     bpos = lsp->clspos;
  116.     if (bflpush(lsp->bp, &bpos) == -1) {
  117.         LSEPRINT;
  118.         return -1;
  119.     }
  120.  
  121.     /* clear modify bit in header */
  122.     lsp->lshdr.flags &= ~LSHMOD;
  123.     if (bputhf(lsp->bp, sizeof(bpos_t),
  124.         (void *)((char *)&lsp->lshdr + sizeof(bpos_t)),
  125.             sizeof(lshdr_t) - sizeof(bpos_t)) == -1) {
  126.         LSEPRINT;
  127.         return -1;
  128.     }
  129.     if (bsync(lsp->bp) == -1) {
  130.         LSEPRINT;
  131.         return -1;
  132.     }
  133.  
  134.     /* position cursor to next record */
  135.     if (lsnext(lsp) == -1) {
  136.         LSEPRINT;
  137.         return -1;
  138.     }
  139.  
  140.     return 0;
  141. }
  142.